package com.yuerenjf.yuerenjinfu.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiniu.android.utils.StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.yuerenjf.yuerenjinfu.R;
import com.yuerenjf.yuerenjinfu.http.Constants;
import com.yuerenjf.yuerenjinfu.http.Xutils;
import com.yuerenjf.yuerenjinfu.utils.CacheUtils;
import com.yuerenjf.yuerenjinfu.utils.CommonUtils;
import com.yuerenjf.yuerenjinfu.utils.ConstantUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

//用户认证信息页面
public class UserAuthActivity extends BaseActivity {

    private LinearLayout mUser_authentication_first_layout;
    private EditText mUser_authentication_edit;
    private EditText mUser_authentication_idcard_edit;
    private TextView mUser_tijiao_text;

    private LinearLayout mUser_authentication_second_layout;
    private TextView mUser_authentication_name_text;
    private TextView mUser_authentication_card_text;
    private TextView mUser_authentication_card_bg_user_text;
    private TextView mUser_authentication_card_bg_number_text;
    private TextView mUser_authentication_card_bg_bind_text;

    private String realname;
    private String userCard;
    private String bankCard;

    boolean isAuth;

    public static void launchActivity(Context context, String realname,String userCard,String bankCard) {
        Intent intent = new Intent(context, UserAuthActivity.class);
        intent.putExtra("realname", realname);
        intent.putExtra("userCard", userCard);
        intent.putExtra("bankCard", bankCard);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isAuth = getIntent().getBooleanExtra("isAuth", false);
        tv_title.setText("身份认证");
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        realname=getIntent().getStringExtra("realname");
        userCard=getIntent().getStringExtra("userCard");
        bankCard=getIntent().getStringExtra("bankCard");


        if(StringUtils.isNullOrEmpty(realname)){
            mUser_authentication_first_layout.setVisibility(View.VISIBLE);
            mUser_authentication_second_layout.setVisibility(View.GONE);

        }else{
            mUser_authentication_first_layout.setVisibility(View.GONE);
            mUser_authentication_second_layout.setVisibility(View.VISIBLE);
            mUser_authentication_name_text.setText(realname);
            mUser_authentication_card_text.setText(userCard);
//            mUser_authentication_card_bg_number_text
            if(StringUtils.isNullOrEmpty(bankCard)){
                mUser_authentication_card_bg_bind_text.setVisibility(View.VISIBLE);
            }else{
                mUser_authentication_card_bg_bind_text.setVisibility(View.GONE);
            }



        }
    }


    private void bindViews(View view) {

        mUser_authentication_first_layout = (LinearLayout) view.findViewById(R.id.user_authentication_first_layout);
        mUser_authentication_edit = (EditText) view.findViewById(R.id.user_authentication_edit);
        mUser_authentication_idcard_edit = (EditText) view.findViewById(R.id.user_authentication_idcard_edit);
        mUser_tijiao_text = (TextView) view.findViewById(R.id.user_tijiao_text);
        mUser_authentication_second_layout = (LinearLayout) view.findViewById(R.id.user_authentication_second_layout);
        mUser_authentication_name_text = (TextView) view.findViewById(R.id.user_authentication_name_text);
        mUser_authentication_card_text = (TextView) view.findViewById(R.id.user_authentication_card_text);
        mUser_authentication_card_bg_user_text = (TextView) view.findViewById(R.id.user_authentication_card_bg_user_text);
        mUser_authentication_card_bg_number_text = (TextView) view.findViewById(R.id.user_authentication_card_bg_number_text);
        mUser_authentication_card_bg_bind_text = (TextView) view.findViewById(R.id.user_authentication_card_bg_bind_text);
    }


    @Override
    protected View getChildView() {
        View view = View.inflate(this, R.layout.activity_user_authentication, null);
        bindViews(view);

        mUser_tijiao_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAuth(mUser_authentication_edit.getText().toString(),mUser_authentication_idcard_edit.getText().toString());
            }
        });

        mUser_authentication_card_bg_bind_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void setAuth(String name,String icCard) {

        Map<String, String> map = new HashMap<>();
        String uid=CacheUtils.getString(UserAuthActivity.this,ConstantUtils.SP_UID);
        String secret=CacheUtils.getString(UserAuthActivity.this,ConstantUtils.SP_SECRET);
        String signature = CommonUtils.md5(CommonUtils.md5("680d76a85b1299667b5dfbe553b8c590"+icCard+name+uid) + secret);
        map.put("signature",signature);
        map.put("uid",CacheUtils.getString(UserAuthActivity.this,ConstantUtils.SP_UID));
        map.put("realname",name);
        map.put("card_id",icCard);
        Xutils.getInstance().post(UserAuthActivity.this, Constants.URL_SETTING_AUTH, map, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);

                    String dataUrl = jsonObject.getString("data");

                    Log.i("dataUrl",dataUrl);
//                    WebContentActivity.launchActivity(UserAuthActivity.this,dataUrl);
                    finish();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailer(int status, String result) {

            }
            @Override
            public void onFinish() {
            }
        });


    }



//    private void getAuthStatus() {
//
//        Map<String, String> map = new HashMap<>();
//        map.put("signature", CacheUtils.getString(UserAuthActivity.this, ConstantUtils.SP_SIGNATURE));
//        map.put("uid",CacheUtils.getString(UserAuthActivity.this,ConstantUtils.SP_UID));
//        Xutils.getInstance().post(UserAuthActivity.this, Constants.URL_SETTING_AUTH, map, new Xutils.XCallBack() {
//            @Override
//            public void onResponse(String result) {
//                try {
//                    JSONObject jsonObject = new JSONObject(result);
//                    String dataUrl = jsonObject.getString("data");
//                    Log.i("dataUrl",dataUrl);
//                    WebActivity.launchActivity(UserAuthActivity.this,dataUrl);
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//            @Override
//            public void onFailer() {
//            }
//
//            @Override
//            public void onFinish() {
//            }
//        });
//
//
//    }



}
